package com.bosssoft.trainee.nontax.service.impl;

import com.bosssoft.trainee.nontax.dao.PermissionDao;
import com.bosssoft.trainee.nontax.dao.ResourceDao;
import com.bosssoft.trainee.nontax.dao.RolePermissionDao;
import com.bosssoft.trainee.nontax.service.PermissionService;
import com.bosssoft.trainee.nontax.entity.dto.PermissionDto;
import com.bosssoft.trainee.nontax.entity.po.Permission;
import com.bosssoft.trainee.nontax.entity.po.Resource;
import com.bosssoft.trainee.nontax.entity.vo.PermissionVo;
import com.bosssoft.trainee.nontax.util.mapper.dtoVo.PermissionDtoVo;
import com.bosssoft.trainee.nontax.util.mapper.poDto.PermissionPoDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * (Permission)表服务实现类
 * @Author: Luo WenLong
 * @since 2022-06-12 10:16:13
 */
@Slf4j
@Service("permissionService")
public class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private ResourceDao resourceDao;

    @Autowired
    private RolePermissionDao rolePermissionDao;

    @Autowired
    private PermissionDtoVo permissionDtoVo;

    @Autowired
    private PermissionPoDto permissionPoDto;

    /**
     * @Description: 获取页面的权限
     * @param pageName 页面名称
     * @return: java.util.Map<java.lang.String,java.util.List<java.lang.String>>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public Map<String, List<String>> getPermissionByPageName(String pageName) {
        Map<String, List<String>> tagToRoles = new TreeMap<>();
        List<Permission> pagePermission = permissionDao.getPagePermission(pageName);
        if(pagePermission == null){
            log.info("get permission by page name failure, no contains this page name");
            return Collections.emptyMap();
        }
        pagePermission.forEach(permission -> tagToRoles.put(
                permission.getOptionTag().replace('-','_'),
                permissionDao.getRolesByPermission(permission.getPermissionId())));
        return tagToRoles;
    }

    /**
     * @Description: 获取async router的权限
     * @return: java.util.Map<java.lang.String,java.util.List<java.lang.String>>
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public Map<String, List<String>> getAsyncRouterPermission() {
        Map<String, List<String>> tagToRoles = new TreeMap<>();
        ArrayList<Resource> resources = resourceDao.queryAll();
        resources.forEach(resource -> tagToRoles.put(
                resource.getResourceTag(),
                rolePermissionDao.getAsyncRouterRolesByPageName(resource.getResourceTag())));
        return tagToRoles;
    }

    /**
     * @Description: 通过ID查询单条数据
     * @param permissionId 主键
     * @return 实例对象
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public PermissionVo queryById(Integer permissionId) {
        return permissionDtoVo.dto2vo(permissionPoDto.po2dto(this.permissionDao.queryById(permissionId)));
    }


    /**
     * @Description: 新增数据
     * @param permission 实例对象
     * @return 实例对象
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public PermissionVo insert(PermissionDto permission) {
        this.permissionDao.insert(permission);
        return permissionDtoVo.dto2vo(permission);
    }

    /**
     * @Description: 修改数据
     * @param permission 实例对象
     * @return 实例对象
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public PermissionVo update(PermissionDto permission) {
        this.permissionDao.update(permission);
        return permissionDtoVo.dto2vo(permissionDtoVo.vo2dto(this.queryById(permission.getPermissionId())));
    }

    /**
     * @Description: 通过主键删除数据
     * @param permissionId 主键
     * @return 是否成功
     * @Author: Luo WenLong
     * @Date: 2022/6/26 13:20
     */
    @Override
    public boolean deleteById(Integer permissionId) {
        return this.permissionDao.deleteById(permissionId) > 0;
    }
}
